package com.zl.shirodemo.service;

import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zl.shirodemo.entity.SysUser;
import com.zl.shirodemo.mapper.SysUserMapper;
import com.zl.shirodemo.requestBean.UserAddBean;

@Service
public class SysUserServiceImpl implements  SysUserService{
    @Autowired
    private SysUserMapper sysUserMapper;
    
    @Value("${shiro.password-hashIterations}")
    private int passwordHashIterations;
    
    
    @Transactional
    public  void  addUser(UserAddBean userAddBean){
        String passwordPassed = userAddBean.getPassword();
        SysUser sysUser = new SysUser();
        sysUser.setUsername(userAddBean.getUsername());
        sysUser.setPassowrd(userAddBean.getPassword());
        sysUser.setRealName(userAddBean.getRealName());
        sysUserMapper.insert(sysUser);
        int userid =  sysUser.getUserid();
        
        
        /**
         * 用userid 作为盐给密码加密
         */
        String hashEdPassword = new Md5Hash(passwordPassed, String.valueOf(userid), passwordHashIterations).toHex();
        sysUser = new SysUser();
        sysUser.setUserid(userid);
        sysUser.setPassowrd(hashEdPassword);
        sysUserMapper.updateByPrimaryKeySelective(sysUser);
    }
}
